<script setup>
import NavBar from './components/NavBar.vue'
import { useUserStore } from './stores/user'
import { onMounted, onBeforeUnmount } from 'vue'
import { checkLoginStatus } from './api/user'

const userStore = useUserStore()
// 初始化用户信息
userStore.initUserInfo()

// 检查用户登录状态
const checkLoginState = async () => {
  try {
    const response = await checkLoginStatus()
    if (response.code === 200 && response.data === false && userStore.id) {
      userStore.clearUserInfo()
    }
  } catch (error) {
    // 错误处理
  }
}

// 定期检查登录状态（每5分钟）
let loginCheckInterval = null

onMounted(() => {
  // 首次加载时检查
  checkLoginState()
  // 设置定期检查
  loginCheckInterval = setInterval(checkLoginState, 5 * 60 * 1000) // 5分钟
})

onBeforeUnmount(() => {
  // 组件卸载时清除定时器
  if (loginCheckInterval) {
    clearInterval(loginCheckInterval)
  }
})
</script>

<template>
  <NavBar />
  <main class="main-content">
    <router-view />
  </main>
</template>

<style>
body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background-color: #f0f2f5;
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

.main-content {
  padding-top: 60px; /* 为固定导航栏留出空间 */
}

.home {
  box-sizing: border-box;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  text-align: center;
  padding-top: 80px;
}

.input-box {
  font-size: 16px;
  padding: 10px 16px;
  min-height: calc(1.5em + 5px);
  height: auto;
  max-height: 150px;
  resize: none;
  overflow-y: auto;
  line-height: 1.5;
  box-sizing: border-box;
}
</style>
